#include <stdio.h>
#include <cups/cups.h>
//#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    // The file to print.

    char *file_to_print = "test.pdf";

    cups_dest_t *dest;
    cups_dest_t *dests;
    int jobid;
    int num_dests;
    const char *value;

    // Get a list of available printer destinations.

    num_dests = cupsGetDests(&dests);
    printf("num_dests = %d\n", num_dests);
    // Get the default printer.

    dest = cupsGetDest(NULL, NULL, num_dests, dests);

    // You can also specify a printer to use.
    // The printer names are in /etc/cups/printers.conf
    //dest = cupsGetDest("Deskjet-460", NULL, num_dests, dests);

    if (dest)
    {
        // The destination exists, but is the printer attached and available?

        printf("Default printer: %s\n", dest->name);

        value = cupsGetOption("printer-state", dest->num_options, dest->options);

        printf("value = %li\n", strtol(value, NULL, 10));

        if (strtol(value, NULL, 10) < IPP_PRINTER_STOPPED)
        {
            // The printer is attached and available. Print the file.

            jobid = cupsPrintFile(
                        dest->name,
                        file_to_print,
                        "Test Job",
                        dest->num_options,
                        dest->options);
            printf("status = %s\n", cupsLastErrorString());
        }
        else
        {
            printf("Printer is stopped. Check printer.\n");
        }
    }
    else
    {
        printf ("No default printer.\n");
    }

    // Finished using the printer.

    cupsFreeDests(num_dests, dests);
    //    printf("Hello World!\n");
    return 0;
}

